﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WI_E2_Parser
{
    class Indexer
    {
        public List<string> crawledPages = new List<string>();
        public List<Term> termIndex = new List<Term>();

        public void addPage(string url, List<string> tokens) {
            crawledPages.Add(url);
            foreach(string t in tokens.Distinct())
            {
                if (termIndex.Where(x => x.termName == t).Count() == 0)
                {
                   termIndex.Add(new Term(t,crawledPages.IndexOf(url),tokens.Count(x => x.Equals(t))));
                }
                else
                {
                    int tmp = termIndex.FindIndex(x => x.termName.Equals(t));
                    termIndex[tmp].addPageToTerm(crawledPages.IndexOf(url), tokens.Count(x => x.Equals(t)));
                }
            }
        }

        public bool contains(string url) {
            return crawledPages.Contains(url);
        }

        public int pageCount(string term)
        {
            foreach (Term t in termIndex.Where(x => x.termName.Equals(term)))
            {
                return t.frequency.Count();
            }

            return 0;
        }
    }
}
